package com.changgou.order.service.impl;

import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.order.pojo.Order;
import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.CartService;
import com.changgou.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/*****
 * @Author: 黑马训练营
 * @Description: com.changgou.order.service.impl
 ****/
@Service
public class CartServiceimpl implements CartService {

    //Redis
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private SpuFeign spuFeign;

    /***
     * 购物车清单列表
     * @param ids
     * @return
     */
    @Override
    public List<OrderItem> list(List<String> ids,String username) {
        //查询Redis中的商品明细信息
        List<OrderItem> orderItems = redisTemplate.boundHashOps("Cart_" + username).values();

        //筛选当前勾选的商品
        List<OrderItem> chooseOrderItems = new ArrayList<OrderItem>();
        for (OrderItem orderItem : orderItems) {
            //循环ids，进行匹配
            for (String id : ids) {
                if(orderItem.getSkuId().equals(id)){
                    chooseOrderItems.add(orderItem);
                    break;
                }
            }
        }
        return chooseOrderItems;
    }

    /***
     * 根据名字查询购物车
     * @param username
     * @return
     */
    @Override
    public List<OrderItem> list(String username) {
        //return redisTemplate.boundHashOps("Cart_"+username).keys();   //获取所有key的值
        return redisTemplate.boundHashOps("Cart_"+username).values(); //获取所有value
    }

    /****
     * 购物车添加
     * @param num
     * @param id
     * @param username
     */
    @Override
    public void add(Integer num, String id, String username) {
        //如果加入购物车的数量<=0，则移除该商品明细
        if(num<=0){
            redisTemplate.boundHashOps("Cart_"+username).delete(id);
            return;
        }

        //一个商品购买的明细信息
        //根据id将sku查询出来
        Result<Sku> skuResult = skuFeign.findById(id);
        Sku sku = skuResult.getData();

        //查询Sku的Spu
        Result<Spu> spuResult = spuFeign.findById(sku.getSpuId());
        Spu spu = spuResult.getData();

        //将Sku和Spu转成OrderItem对象
        OrderItem orderItem = sku2OrderItem(sku, spu, num);

        //直接往Redis中增加商品数据即可
        //     key1:username
        //                 key2:id  value: 每条商品加入购物车的明细记录
        redisTemplate.boundHashOps("Cart_"+username).put(id,orderItem);
    }


    /***
     * @param sku
     * @param spu
     * @param num:购买数量
     * @return
     */
    public OrderItem sku2OrderItem(Sku sku,Spu spu,Integer num){
        OrderItem orderItem = new OrderItem();
        orderItem.setCategoryId1(spu.getCategory1Id());
        orderItem.setCategoryId2(spu.getCategory2Id());
        orderItem.setCategoryId3(spu.getCategory3Id());
        orderItem.setSpuId(spu.getId());
        orderItem.setSkuId(sku.getId());
        orderItem.setName(sku.getName());
        orderItem.setPrice(sku.getPrice());
        orderItem.setNum(num);
        orderItem.setMoney(num*orderItem.getPrice());
        orderItem.setImage(spu.getImage());
        return orderItem;
    }

}
